package com.basic.emg.controller;


import com.alibaba.fastjson.JSON;
import com.basic.common.result.WebResult;
import com.basic.emg.domain.EmergencyDispose;
import com.basic.emg.domain.EmergencyMaterial;
import com.basic.emg.domain.EmergencyPlan;
import com.basic.emg.domain.EmergencyTeam;
import com.basic.emg.service.EmergencyDisposeService;
import com.basic.emg.service.EmergencyMaterialService;
import com.basic.emg.service.EmergencyPlanService;
import com.basic.emg.service.EmergencyTeamService;
import com.github.pagehelper.PageInfo;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import java.nio.charset.StandardCharsets;
import java.util.List;


@RestController
@RequestMapping("/emergency")
@Slf4j
public class EmergencyController extends BaseController{


    @Autowired
    private EmergencyPlanService planService;

    @Autowired
    private EmergencyTeamService teamService;

    @Autowired
    private EmergencyMaterialService materialService;

    @Autowired
    private EmergencyDisposeService disposeService;


    /**
     * 条件查询应急预案
     * @param
     * @return
     */
    @GetMapping("/selectEmergencyPlan")
    public WebResult selectEmergencyPlan(EmergencyPlan plan) throws Exception {
        startPage(plan);
        List<EmergencyPlan> emergencyPlans = planService.selectPlanList(plan);
        long total = new PageInfo(emergencyPlans).getTotal();
        return WebResult.success(emergencyPlans,total);
    }

    /**
     * 根据id查询预案
     * @param
     * @return
     */
    @GetMapping("/selectPlanById")
    public WebResult selectPlanById(Long id) throws Exception {
        EmergencyPlan emergencyPlan = planService.selectPlanById(id);
        return WebResult.success(emergencyPlan);
    }

    /**
     * 创建预案
     * @param
     * @return
     */
    @PostMapping("/createPlan")
    public WebResult createPlan(@RequestPart(name="file",required = false) MultipartFile[] file,
                                @RequestPart("entity") String entity) throws Exception {
        entity = new String(entity.getBytes(StandardCharsets.ISO_8859_1), StandardCharsets.UTF_8);
        EmergencyPlan plan = JSON.parseObject(entity, EmergencyPlan.class);
        planService.createPlan(plan,file);
        return WebResult.success();
    }


    /**
     * 编辑预案
     * @param
     * @return
     */
    @PutMapping("/editPlan")
    public WebResult editPlan(@RequestPart(name="file",required = false) MultipartFile[] file,
                              @RequestPart("entity") String entity) throws Exception {
        entity = new String(entity.getBytes(StandardCharsets.ISO_8859_1), StandardCharsets.UTF_8);
        EmergencyPlan plan = JSON.parseObject(entity, EmergencyPlan.class);
        planService.editPlan(plan,file);
        return WebResult.success();
    }

    /**
     * 删除预案
     * @param
     * @return
     */
    @DeleteMapping("/deletePlan")
    public WebResult deletePlan(@RequestParam List<Long> ids) throws Exception {
        planService.deletePlan(ids);
        return WebResult.success();
    }



    /**
     * 条件查询应急队伍
     * @param
     * @return
     */
    @GetMapping("/selectEmergencyTeam")
    public WebResult selectEmergencyTeam(EmergencyTeam team) throws Exception {
        startPage(team);
        List<EmergencyTeam> emergencyTeams = teamService.selectTeamList(team);
        long total = new PageInfo(emergencyTeams).getTotal();
        return WebResult.success(emergencyTeams,total);
    }

    /**
     * 根据id查询队伍
     * @param
     * @return
     */
    @GetMapping("/selectTeamById")
    public WebResult selectTeamById(Long id) throws Exception {
        EmergencyTeam team = teamService.selectTeamById(id);
        return WebResult.success(team);
    }

    /**
     * 创建应急队伍
     * @param
     * @return
     */
    @PostMapping("/createTeam")
    public WebResult createTeam(@RequestBody EmergencyTeam team) throws Exception {
        teamService.createTeam(team);
        return WebResult.success();
    }


    /**
     * 编辑队伍
     * @param
     * @return
     */
    @PutMapping("/editTeam")
    public WebResult editTeam(@RequestBody EmergencyTeam team) throws Exception {
        teamService.editTeam(team);
        return WebResult.success();
    }

    /**
     * 删除队伍
     * @param
     * @return
     */
    @DeleteMapping("/deleteTeam")
    public WebResult deleteTeam(@RequestParam("ids") List<Long> ids) throws Exception {
        teamService.deleteTeam(ids);
        return WebResult.success();
    }

    
    /**
     * 条件查询应急物资
     * @param
     * @return
     */
    @GetMapping("/selectEmergencyMaterial")
    public WebResult selectEmergencyMaterial(EmergencyMaterial material) throws Exception {
        startPage(material);
        List<EmergencyMaterial> emergencyMaterials = materialService.selectMaterialList(material);
        long total = new PageInfo(emergencyMaterials).getTotal();
        return WebResult.success(emergencyMaterials,total);
    }

    /**
     * 根据id查询物资
     * @param
     * @return
     */
    @GetMapping("/selectMaterialById")
    public WebResult selectMaterialById(Long id) throws Exception {
        EmergencyMaterial material = materialService.selectMaterialById(id);
        return WebResult.success(material);
    }

    /**
     * 创建应急物资
     * @param
     * @return
     */
    @PostMapping("/createMaterial")
    public WebResult createMaterial(@RequestBody EmergencyMaterial material) throws Exception {
        materialService.createMaterial(material);
        return WebResult.success();
    }


    /**
     * 编辑物资
     * @param
     * @return
     */
    @PutMapping("/editMaterial")
    public WebResult editMaterial(@RequestBody EmergencyMaterial material) throws Exception {
        materialService.editMaterial(material);
        return WebResult.success();
    }

    /**
     * 删除物资
     * @param
     * @return
     */
    @DeleteMapping("/deleteMaterial")
    public WebResult deleteMaterial(@RequestParam List<Long> ids) throws Exception {
        materialService.deleteMaterial(ids);
        return WebResult.success();
    }

    /**
     * 条件查询应急处置
     * @param
     * @return
     */
    @GetMapping("/selectEmergencyDispose")
    public WebResult selectEmergencyDispose(EmergencyDispose dispose) throws Exception {
        startPage(dispose);
        List<EmergencyDispose> emergencyDisposes = disposeService.selectDisposeList(dispose);
        long total = new PageInfo(emergencyDisposes).getTotal();
        return WebResult.success(emergencyDisposes,total);
    }

    /**
     * 根据id查询处置
     * @param
     * @return
     */
    @GetMapping("/selectDisposeById")
    public WebResult selectDisposeById(Long id) throws Exception {
        EmergencyDispose dispose = disposeService.selectDisposeById(id);
        return WebResult.success(dispose);
    }

    /**
     * 创建应急处置
     * @param
     * @return
     */
    @PostMapping("/createDispose")
    public WebResult createDispose(@RequestPart(name="file",required = false) MultipartFile[] file,
                                   @RequestPart("entity") String entity) throws Exception {
        entity = new String(entity.getBytes(StandardCharsets.ISO_8859_1), StandardCharsets.UTF_8);
        EmergencyDispose dispose = JSON.parseObject(entity, EmergencyDispose.class);
        disposeService.createDispose(dispose,file);
        return WebResult.success();
    }


    /**
     * 编辑应急处置
     * @param
     * @return
     */
    @PutMapping("/editDispose")
    public WebResult editDispose(@RequestPart(name="file",required = false)MultipartFile[] file,
                                 @RequestPart("entity") String entity) throws Exception {
        entity = new String(entity.getBytes(StandardCharsets.ISO_8859_1), StandardCharsets.UTF_8);
        EmergencyDispose dispose = JSON.parseObject(entity, EmergencyDispose.class);
        disposeService.editDispose(dispose,file);
        return WebResult.success();
    }

    /**
     * 删除物资
     * @param
     * @return
     */
    @DeleteMapping("/deleteDispose")
    public WebResult deleteDispose(@RequestParam List<Long> ids) throws Exception {
        disposeService.deleteDispose(ids);
        return WebResult.success();
    }





}






